<?php
  /**
  Author: shielamarie
  Date: Oct 2011
  **/
	class StudentController
	{
		private $d;
		public function __construct()
		{
			$this->d = new DbHelper();
		}
		public function create($student)
		{
			$i = $student['id'];
			$n = $student['name'];
			$c = $student['section'];
			$p = $student['payment'];
			$s = "INSERT INTO Students VALUES ('$i', '$n', '$c', '$p')";
			return $this->d->query($s);
		}
		public function read($studentId)
		{
			$s = "SELECT * FROM Students WHERE StudentId = $studentId";
			return $this->d->query($s);
		}
		public function readElementaryDetails($studentId)
		{
		  $query = "SELECT * FROM ElementaryDetails WHERE StudentId = $studentId";
		  return $this->d->query($query);
		}
		public function readPreElementaryDetails($studentId)
		{
		  $query = "SELECT * FROM PreElementaryDetails WHERE StudentId = $studentId";
		  return $this->d->query($query);
		}
		public function update($student)
		{
			$i = $student['id'];
			$n = $student['name'];
			$c = $student['section'];
			$b = $student['balance'];
			$p = $student['payment'];
			$remainingBalance = (int)$b - (int)$p;
			$s = "UPDATE Students SET Id='$i', Name='$n', Section='$c', " .
					"Balance='$remainingBalance' WHERE Id = $i";
			$this->d->query($s);
		}
		public function updatePassword($password)
		{
		  session_start();
		  $u = $_SESSION['loggedUser'];
		  $uid = $u['UserId'];
		  //
		  $query = "UPDATE Users SET Password= '$password' WHERE UserId='$uid'";
		  $this->d->query($query);
		}
		public function delete($studentId)
		{
			$s = "DELETE FROM Students WHERE Id = $studentId";
			$this->d->query($s);
		}
		public function getStudentCount($departmentId) {
		  $query = "SELECT COUNT(`Id`) FROM Students
		            WHERE DepartmentId = '$departmentId'";
		  return $this->d->query($query);
		}
		public function login($id, $password)
		{
		  $s = "SELECT userId FROM Users " .
		        "WHERE userType=$id AND userPassword=md5('".$password."')";
		  $n = $this->d->query($s) > 0;
		  return $n ? true : false;
		}
		public function getDepartmentList()
		{
		  $query = "SELECT * FROM Departments";
		  return $this->d->query($query);
		}
		public function addYearLevel($yearLevelName,$departmentId)
		{
		  $query = "INSERT INTO YearLevels(YearLevelDescription,DepartmentId)
		            VALUES('$yearLevelName','$departmentId')";
		  return $this->d->query($query);
		}
		public function getYearLevel()
		{
		  $query = "SELECT * FROM YearLevels";
		  return $this->d->query($query);
		}
		public function getYearLevelByDepartment($departmentId)
		{
		  $query = "SELECT * FROM YearLevels
		            WHERE DepartmentId = $departmentId";
		  return $this->d->query($query);
		}
		public function addSubject($subjectName,$yearLevel)
		{
			$query = "INSERT INTO Subjects(SubjectName,YearLevelId)
                VALUES('$subjectName','$yearLevel')";
			return $this->d->query($query);
		}
		public function getSubjectList()
		{
		  $query = "SELECT Subjects.YearLevelId, YearLevels.YearLevelDescription, 
					Subjects.SubjectName
					FROM YearLevels
					INNER JOIN Subjects
					ON YearLevels.YearLevelId = Subjects.YearLevelId
					ORDER BY Subjects.YearLevelId DESC";
		  return $this->d->query($query);
		}
		public function addTeacher($teachersName, $yearLevel)
		{
		  $query = "INSERT INTO Teachers(TeachersName,YearLevelId)
					VALUES('$teachersName','$yearLevel')";
		  return $this->d->query($query);
		}
		public function getTeacherList()
		{
		  $query = "SELECT Teachers.TeacherId, Teachers.TeachersName, 
					YearLevels.YearLevelDescription
					FROM Teachers
					INNER JOIN YearLevels
					ON Teachers.YearLevelId = YearLevels.YearLevelId
					ORDER BY Teachers.TeachersName ASC";
		  return $this->d->query($query);
		}
		public function getTeacherDetails($tid)
		 {
		  $query = "SELECT * FROM Teachers
					WHERE TeacherId = $tid";
		  return $this->d->query($query);
		}
		public function updateTeacherDetails($tid,$teachersName,$yid)
		{
		  $query = "UPDATE Teachers SET TeachersName='$teachersName', 
					YearLevelId = '$yid'
					WHERE TeacherId= '$tid'";
		  $this->d->query($query);
		}
		public function getFeesList()
		{
		  $query = "SELECT Fees.FeeId, Fees.DepartmentId, Fees.FeeName, 
					Fees.FeeAmount,Departments.DepartmentName
					FROM Fees
					INNER JOIN Departments
					ON Fees.DepartmentId = Departments.DepartmentId";
		  return $this->d->query($query);
		}
		public function addFees($feeName,$feeAmount,$departmentId)
		{
		  $query = "INSERT INTO Fees(FeeName,FeeAmount,DepartmentId)
					VALUES('$feeName','$feeAmount','$departmentId')";
		  return $this->d->query($query);
		}
		public function getFees($fid)
		{
		  $query = "SELECT * FROM Fees
					WHERE Fees.FeeId = '$fid'";
		  return $this->d->query($query);
		}
		public function getFeesByDepartment($departmentId) {
		  $query = "SELECT FeeName,FeeAmount
					FROM Fees
					WHERE DepartmentId = '$departmentId'";
		  return $this->d->query($query);
		}
		public function updateFees($id,$feeName, $feeAmount, $departmentId)
		{
		  $query = "UPDATE Fees SET FeeName='$feeName', 
					FeeAmount = '$feeAmount', 
					DepartmentId= '$departmentId'
					WHERE FeeId = '$id'";
		  return $this->d->query($query);
		}
		public function removeFees($id) {
		  $query = "DELETE FROM Fees
					WHERE Fees.FeeId = '$id'";
		  return $this->d->query($query);
		}
		public function getStudentDetails($id)
		{
		  $query = "SELECT * FROM Students
					WHERE StudentId='$id'";
		  return $this->d->query($query);
		}
		public final function getStudentIsEnroled($id, $schoolYear)
		{
			$sy = "(SELECT Id FROM SchoolYears WHERE Year = $schoolYear)";
			$s = "SELECT COUNT(Id) FROM Students WHERE Id = $studentId AND
					SchoolYearId = $sy";
			$i = mysql_result($this->d->query($s), 0);
			return $i > 0;
		}
	}
?>